package com.pucheng.bracelet.ui.activity.watch

import android.os.Bundle
import androidx.lifecycle.observe
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.util.click
import com.pucheng.bracelet.databinding.ActivityWatchBinding
import com.pucheng.bracelet.ui.adapter.WatchAdapter
import com.pucheng.bracelet.ui.adapter.WatchData
import com.pucheng.bracelet.viewmodel.bracelet.WatchViewModel
import kotlinx.android.synthetic.main.activity_watch.*
import kotlinx.android.synthetic.main.layout_title2.*
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity

class WatchActivity : BaseVmDbActivity<WatchViewModel, ActivityWatchBinding>() {
    private var fragment: WatchEditFragment? = null
    private val mAdapter = WatchAdapter ({ _, data: WatchData ->
        if (!data.selected) {
            mViewModel.selectWatchData(data.id)
        }
    },
    { _: Int, watchData: WatchData ->
        fragment = WatchEditFragment.newInstance( watchData) {
            supportFragmentManager.beginTransaction().apply {
                fragment?.let {
                    hide(it)
                    commit()
                }
            }
        }
        supportFragmentManager.beginTransaction().apply {
            fragment?.let {
                replace(R.id.fl_watch_edit, it)
                commit()
            }
        }
    })

    override fun layoutId(): Int = R.layout.activity_watch
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.apply {
            viewModel = mViewModel
            mDatabind.lifecycleOwner = this@WatchActivity
            rvWatch.apply {
                adapter = mAdapter
                layoutManager = LinearLayoutManager(this@WatchActivity)
            }
            ibtn_back.apply {
                setOnClickListener {
                    finish()
                }
            }
            tv_title.apply {
                text = "表盘选择"
                setTextColor(this@WatchActivity.resources.getColor(R.color.black))
            }
            tv_tips.click { ToastUtils.showShort("暂无更多") }
        }
        mViewModel.watchDatas.observe(this) {
            mAdapter.submitList(it)
        }
        mViewModel.getWatchData()
    }

    override fun createObserver() {
    }

    override fun dismissLoading() {
    }

    override fun showLoading(message: String) {
    }
}